package com.sz.admin.system.controller;

import com.sz.admin.system.pojo.dto.filemonitor.SysFileMonitorCreateDTO;
import com.sz.admin.system.pojo.dto.filemonitor.SysFileMonitorListDTO;
import com.sz.admin.system.pojo.dto.filemonitor.SysFileMonitorUpdateDTO;
import com.sz.admin.system.pojo.vo.filemonitor.SysFileMonitorVO;
import com.sz.admin.system.pojo.vo.filemonitor.SysMonitorFileRecordVO;
import com.sz.admin.system.service.FolderMonitorService;
import com.sz.admin.system.service.SysFileMonitorService;
import com.sz.admin.system.service.SysMonitorFileRecordService;
import com.sz.core.common.entity.ApiResult;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 文件监控管理控制器
 *
 * @author sz-admin
 * @since 2025-01-13
 */
@Tag(name = "文件监控管理")
@RestController
@RequestMapping("/system/fileMonitor")
@RequiredArgsConstructor
public class SysFileMonitorController {

    private final SysFileMonitorService sysFileMonitorService;
    private final SysMonitorFileRecordService recordService;
    private final FolderMonitorService folderMonitorService;

    @Operation(summary = "创建文件监控")
    @PostMapping
    public ApiResult<Void> create(@Valid @RequestBody SysFileMonitorCreateDTO dto) {
        sysFileMonitorService.create(dto);
        return ApiResult.success();
    }

    @Operation(summary = "更新文件监控")
    @PutMapping
    public ApiResult<Void> update(@Valid @RequestBody SysFileMonitorUpdateDTO dto) {
        sysFileMonitorService.update(dto);
        return ApiResult.success();
    }

    @Operation(summary = "文件监控分页列表")
    @PostMapping("/page")
    public ApiResult<PageResult<SysFileMonitorVO>> page(@RequestBody SysFileMonitorListDTO dto) {
        PageResult<SysFileMonitorVO> result = sysFileMonitorService.page(dto);
        return ApiResult.success(result);
    }

    @Operation(summary = "文件监控列表")
    @PostMapping("/list")
    public ApiResult<List<SysFileMonitorVO>> list(@RequestBody SysFileMonitorListDTO dto) {
        List<SysFileMonitorVO> result = sysFileMonitorService.list(dto);
        return ApiResult.success(result);
    }

    @Operation(summary = "删除文件监控")
    @DeleteMapping
    public ApiResult<Void> remove(@Valid @RequestBody SelectIdsDTO dto) {
        sysFileMonitorService.remove(dto);
        return ApiResult.success();
    }

    @Operation(summary = "获取文件监控详情")
    @GetMapping("/{id}")
    public ApiResult<SysFileMonitorVO> detail(@PathVariable Object id) {
        SysFileMonitorVO result = sysFileMonitorService.detail(id);
        return ApiResult.success(result);
    }

    @Operation(summary = "启用监控")
    @PostMapping("/enable/{id}")
    public ApiResult<Void> enableMonitor(@PathVariable Long id) {
        sysFileMonitorService.enableMonitor(id);
        folderMonitorService.startMonitor(id);
        return ApiResult.success();
    }

    @Operation(summary = "停用监控")
    @PostMapping("/disable/{id}")
    public ApiResult<Void> disableMonitor(@PathVariable Long id) {
        sysFileMonitorService.disableMonitor(id);
        folderMonitorService.stopMonitor(id);
        return ApiResult.success();
    }

    @Operation(summary = "启动所有监控")
    @PostMapping("/startAll")
    public ApiResult<Void> startAllMonitors() {
        folderMonitorService.startAllMonitors();
        return ApiResult.success();
    }

    @Operation(summary = "停止所有监控")
    @PostMapping("/stopAll")
    public ApiResult<Void> stopAllMonitors() {
        folderMonitorService.stopAllMonitors();
        return ApiResult.success();
    }

    @Operation(summary = "获取监控文件处理记录")
    @GetMapping("/records/{monitorId}")
    public ApiResult<List<SysMonitorFileRecordVO>> getRecords(@PathVariable Long monitorId) {
        List<SysMonitorFileRecordVO> result = recordService.getRecordsByMonitorId(monitorId);
        return ApiResult.success(result);
    }

    @Operation(summary = "手动触发指定监控")
    @PostMapping("/trigger/{id}")
    public ApiResult<Void> triggerMonitor(@PathVariable Long id) {
        folderMonitorService.stopMonitor(id);
        folderMonitorService.startMonitor(id);
        return ApiResult.success();
    }

    @Operation(summary = "记录当前监控状态（调试用）")
    @PostMapping("/logState")
    public ApiResult<Void> logMonitoringState() {
        folderMonitorService.logMonitoringState();
        return ApiResult.success();
    }

}